package com.xin.springcloudconsumer.controller;

import com.xin.springcloudconsumer.dto.Event;
import com.xin.springcloudconsumer.dto.PageResult;
import com.xin.springcloudconsumer.feigns.EventFeign;
import com.xin.springcloudconsumer.form.EventForm;
import com.xin.springcloudconsumer.service.EventService;
import com.xin.springcloudconsumer.utils.ResultVOUtils;
import com.xin.springcloudconsumer.utils.SessionValueUtils;
import com.xin.springcloudconsumer.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@Controller
@RequestMapping("/event")
@Slf4j
public class EventController {
    @Autowired
    private EventService eventService;

    /**
     * 创建事件
     *
     * @param eventForm
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/create")
    public ModelAndView create(@Valid EventForm eventForm,
                               BindingResult bindingResult,
                               Map<String, Object> map,
                               HttpServletRequest request) {
        if (bindingResult.hasErrors()   ) {

            String returnUrl = "/event/findAll";
            map.put("returnUrl", returnUrl);
            map.put("message", bindingResult.getFieldError().getDefaultMessage());
            return new ModelAndView("common/msg_error", map);
        }
        String openId = SessionValueUtils.getOpenId(request);
        eventForm.setOpenId(openId);
        Event event = eventService.create(eventForm);
        if (ObjectUtils.isEmpty(event)) {
            String returnUrl = "/event/findAll";
            map.put("returnUrl", returnUrl);
            map.put("message", "创建事件失败!");
            return new ModelAndView("common/msg_error", map);
        }
        return new ModelAndView("redirect:/event/findAll");
    }

    /**
     * 修改事件
     *
     * @param eventForm
     * @param id
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/update")
    public ModelAndView update(@Valid EventForm eventForm,
                               BindingResult bindingResult,
                               @RequestParam(name = "id", defaultValue = "") String id,
                               Map<String, Object> map,
                               HttpServletRequest request) {
        map.put("returnUrl", "/event/findAll");
        if (bindingResult.hasErrors()) {

            map.put("message", bindingResult.getFieldError().getDefaultMessage());
            return new ModelAndView("common/msg_error", map);
        }
        if (StringUtils.isEmpty(eventForm.getTime())){
            map.put("message","时间不能为空!");
            return new ModelAndView("common/msg_error", map);
        }
        String openId = SessionValueUtils.getOpenId(request);
        eventForm.setOpenId(openId);
        Event event = eventService.update(eventForm, id);
        if (ObjectUtils.isEmpty(event)) {
            log.info("【修改事件】{}", bindingResult.getFieldError().getDefaultMessage());
            map.put("message", bindingResult.getFieldError().getDefaultMessage());
            return new ModelAndView("common/msg_error", map);
        }
        return new ModelAndView("redirect:/event/findAll");
    }

    @GetMapping("/delete")
    public ModelAndView delete(@RequestParam(name = "id", defaultValue = "") String id,
                               Map<String, Object> map) {
        boolean delete = eventService.delete(id);
        map.put("returnUrl", "/event/findAll");
        if (!delete) {
            map.put("message", "删除失败!");
            return new ModelAndView("common/msg_error", map);
        }
        map.put("message", "删除成功!");
        return new ModelAndView("common/msg_success", map);
    }

    /**
     * 查询某个事件
     *
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/findOne")
    public ModelAndView findOne(@RequestParam(name = "id", defaultValue = "") String id,
                                Map<String, Object> map,
                                HttpServletRequest request) {
        Event event = eventService.findOne(id);
        if (ObjectUtils.isEmpty(event)) {
            map.put("returnUrl", "/event/findAll");
            map.put("message", "事件不存在!");
            return new ModelAndView("common/msg_error", map);
        }
        String headImage = SessionValueUtils.getHeadImage(request);
        map.put("myevent", event);
        map.put("headImage", headImage);
        return new ModelAndView("detail/update", map);
    }

    /**
     * 查询全部事件
     *
     * @param pageable
     * @param map
     * @return
     */
    @GetMapping("/findAll")
    public ModelAndView findAll(@PageableDefault(page = 1, size = 10) Pageable pageable,
                                Map<String, Object> map,
                                HttpServletRequest request) {
        String openId = SessionValueUtils.getOpenId(request);
        String headImage = SessionValueUtils.getHeadImage(request);
        PageResult pageResult = eventService.findAll(openId, pageable);
        map.put("pageResult", pageResult);
        map.put("headImage", headImage);
        return new ModelAndView("list/list", map);
    }

    /**
     * 查询全部事件(JSON)
     *
     * @param pageable
     * @param map
     * @return
     */
    @GetMapping("/findAllJSON")
    @ResponseBody
    public ResultVO findAllJSON(@PageableDefault(page = 1, size = 10) Pageable pageable,
                                Map<String, Object> map,
                                HttpServletRequest request) {
        String openId = SessionValueUtils.getOpenId(request);
        PageResult pageResult = eventService.findAll(openId, pageable);
        return ResultVOUtils.success(pageResult);
    }


}
